package cn.tedu.xuecha.controller;


import cn.tedu.xuecha.pojo.dto.ContentAddNewDTO;
import cn.tedu.xuecha.pojo.entity.Content;
import cn.tedu.xuecha.pojo.vo.ContentListItemVO;
import cn.tedu.xuecha.service.IContentService;
import cn.tedu.xuecha.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "01. 内容表管理模块")
@Validated
@RestController
@RequestMapping("/contents")
public class ContentController {

    @Autowired
    IContentService contentService;

    public ContentController() {
        log.info("创建控制器对象：AdminController");
    }


    @ApiOperation("添加内容")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(ContentAddNewDTO contentAddNewDTO) {
        log.debug("开始处理【添加内容表】的请求，参数：{}", contentAddNewDTO);
        contentService.addNew(contentAddNewDTO);
        return JsonResult.ok();
    }


    @ApiOperation("根据id删除内容")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "内容表id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除内容表失败，尝试删除的内容表的ID无效！")
                                   @PathVariable Integer id) {
        log.debug("开始处理【根据id删除内容表】的请求，参数：{}", id);
        contentService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id修改内容")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "内容表id", required = true, dataType = "long")
    @PostMapping("/update")
    public JsonResult<Void> update(Content content) {
        log.debug("开始处理【根据id修改内容表】的请求，参数：{}", content);
        contentService.update(content);
        return JsonResult.ok();
    }
    // http://localhost:9081/contents
    @ApiOperation("查询内容列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<ContentListItemVO>> list(
    ) {
        log.debug("开始处理【查询内容表列表】的请求，无参数");
        List<ContentListItemVO> list = contentService.list();
        return JsonResult.ok(list);
    }


}